package com.hs.panghu.dao.impl;

import com.hs.panghu.dao.CourierStationDao;
import com.hs.panghu.entity.CourierStation;
import com.hs.panghu.entity.Goods;
import com.hs.panghu.util.JDBCDruidUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CourierStationDaoImpl implements CourierStationDao {


    @Override
    public int modifyCourierStation(Integer courierStationId) {
        String sql="update courierStation set courierStationStatus=abs(courierStationStatus-1) where courierStationId=?";

        Object[]objects= {courierStationId};
        int row= JDBCDruidUtil.update(sql, objects);

        return row;
    }

    @Override
    public int insertCourierStation(CourierStation courierStation) {
        String sql="insert into courierStation(courierStationName, courierStationImage, " +
                "courierStationProvince,courierStationCity, courierStationDistrict," +
                " courierStationAddress, addTime,courierStationSort, " +
                "courierStationCommunityId, courierStationCommunityName, " +
                "courierStationOrderCount) values (?,?,?,?,?,?,now(),?,?,?,?)";
        Object []object = {
                courierStation.getCourierStationName(),
                courierStation.getCourierStationImage(),
                courierStation.getCourierStationProvince(),
                courierStation.getCourierStationCity(),
                courierStation.getCourierStationDistrict(),
                courierStation.getCourierStationAddress(),
                courierStation.getCourierStationSort(),
                courierStation.getCourierStationCommunityId(),
                courierStation.getCourierStationCommunityName(),
                courierStation.getCourierStationOrderCount()
        };
        int row = JDBCDruidUtil.update(sql, object);
        return row;
    }

    @Override
    public int updateCourierStation(CourierStation courierStation) {
        String sql="update courierStation " +
                "set courierStationName = ?,courierStationImage=?," +
                "courierStationProvince=?," +
                "courierStationCity=?,courierStationDistrict=?," +
                "courierStationAddress=?,courierStationSort=?," +
                "courierStationCommunityId=?,courierStationCommunityName=?," +
                "courierStationOrderCount=?" +
                " where courierStationId=?";
        System.out.println(sql);
        Object []object = {
                courierStation.getCourierStationName(),
                courierStation.getCourierStationImage(),
                courierStation.getCourierStationProvince(),
                courierStation.getCourierStationCity(),
                courierStation.getCourierStationDistrict(),
                courierStation.getCourierStationAddress(),
                courierStation.getCourierStationSort(),
                courierStation.getCourierStationCommunityId(),
                courierStation.getCourierStationCommunityName(),
                courierStation.getCourierStationOrderCount(),
                courierStation.getCourierStationId()
        };
        int row = JDBCDruidUtil.update(sql, object);
        return row;
    }

    @Override
    public int deleteCourierStation(Integer CourierStationId) {
        int row=0;

        return row;
    }

    @Override
    public List<CourierStation> selectCourierStations(Map<String, String> map) {
        List<CourierStation> list = new ArrayList<>();
        StringBuilder sb = new StringBuilder("select*from courierStation where 1=1 ");
        List params = new ArrayList();


        //驿站名称
        String courierStationName = map.get("courierStationName");
        if (courierStationName != null && !"".equals(courierStationName)) {
            sb.append(" and courierStationName like ? ");
            params.add("%" + courierStationName + "%");
        }
        //小区名称
        String communityName = map.get("communityName");
        if (communityName != null && !"".equals(communityName)) {
            sb.append(" and courierStationCommunityName like ? ");
            params.add("%" + communityName + "%");
        }
        //驿站加小区名字
        String courierStationCommunityName = map.get("courierStationCommunityName");
        if (courierStationCommunityName != null && !"".equals(courierStationCommunityName)) {
            sb.append(" and courierStationCommunityName=? ");
            params.add(courierStationCommunityName);
        }
        //日期
        String startTime = map.get("startTime");
        String endTime = map.get("endTime");
        if (startTime != null && !"".equals(startTime)) {
            sb.append(" and addTime between ? and ? ");
            params.add(startTime);
            params.add(endTime);
        }
        //驿站状态
        String courierStationStatus = map.get("courierStationStatus");
        if (courierStationStatus != null && !"".equals(courierStationStatus)) {
            sb.append(" and courierStationStatus=? ");
            params.add(courierStationStatus);
        }




        //分页
        Integer pageNum = Integer.parseInt(map.get("pageNum"));
        Integer pageSize = Integer.parseInt(map.get("pageSize"));

        sb.append(" limit ?,? ");
        params.add((pageNum - 1) * pageSize);
        params.add(pageSize);

        String sql = sb.toString();
        System.out.println(sql);
        Object[] objects = params.toArray();

        ResultSet query = JDBCDruidUtil.query(sql, objects);

        try {
            while (query.next()) {
                CourierStation courierStation = new CourierStation(
                        query.getInt(1),
                        query.getString(2),
                        query.getString(3),
                        query.getString(4),
                        query.getString(5),
                        query.getString(6),
                        query.getString(7),
                        query.getInt(8),
                        query.getDate(9),
                        query.getInt(10),
                        query.getInt(11),
                        query.getString(12),
                        query.getInt(13)
                );
                list.add(courierStation);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCDruidUtil.close();
        }

        return list;
    }

    @Override
    public int selectCourierStationCount(Map<String, String> map) {
        int count = 0;
        StringBuilder sb = new StringBuilder("select count(1) from courierStation where 1=1 ");
        List params = new ArrayList();


        //驿站名称
        String courierStationName = map.get("courierStationName");
        if (courierStationName != null && !"".equals(courierStationName)) {
            sb.append(" and courierStationName like ? ");
            params.add("%" + courierStationName + "%");
        }
        //小区名称
        String communityName = map.get("communityName");
        if (communityName != null && !"".equals(communityName)) {
            sb.append(" and courierStationCommunityName like ? ");
            params.add("%" + communityName + "%");
        }
        //驿站加小区名字
        String courierStationCommunityName = map.get("courierStationCommunityName");
        if (courierStationCommunityName != null && !"".equals(courierStationCommunityName)) {
            sb.append(" and courierStationCommunityName=? ");
            params.add(courierStationCommunityName);
        }
        //日期
        String startTime = map.get("startTime");
        String endTime = map.get("endTime");
        if (startTime != null && !"".equals(startTime)) {
            sb.append(" and addTime between ? and ? ");
            params.add(startTime);
            params.add(endTime);
        }
        //驿站状态
        String courierStationStatus = map.get("courierStationStatus");
        if (courierStationStatus != null && !"".equals(courierStationStatus)) {
            sb.append(" and courierStationStatus=? ");
            params.add(courierStationStatus);
        }


        //分页
        Integer pageNum = Integer.parseInt(map.get("pageNum"));
        Integer pageSize = Integer.parseInt(map.get("pageSize"));

        sb.append(" limit ?,? ");
        params.add((pageNum - 1) * pageSize);
        params.add(pageSize);

        String sql = sb.toString();
        System.out.println(sql);
        Object[] objects = params.toArray();

        ResultSet query = JDBCDruidUtil.query(sql, objects);

        try {
            while (query.next()) {

                count = query.getInt(1);


            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCDruidUtil.close();
        }
        return count;

    }
}
